/**
 * Created with JetBrains WebStorm.
 * User: huwei
 * Date: 13-8-22
 * Time: 下午1:09
 * To change this template use File | Settings | File Templates.
 */
Ext.ux.WindowAlwaysOnTop = function() {
    this.init = function(win) {
        win.on('deactivate', function() {
            var i = 1;
            this.manager.each(function() {
                i++
            });
            this.setZIndex(this.manager.zseed + (i * 10));
        })
    }
}

function showHandlerWin(url, title, width, height) {
    if (typeof(width) == 'undefined') {
        width = "100%";
    }
    if (typeof(height) == 'undefined') {
        height = "100%";
    }
    win = null;
    if (!win) {
        win = new Ext.Window({
            title: title,
            header: {
                titlePosition: 2,
                titleAlign: 'center'
            },
            closable: true,
            closeAction: 'hide',
            x: 0,
            y: 0,
            width: width,
            height: height,
            html: '<iframe width="100%" height="100%" frameborder="0" src="' + url + '"></iframe>'
        });
    }
    win.show();

}

function doEdit(id_key,grid, _url, title, width, height) {
    var orows = grid.getSelectionModel().getSelection();
    if (orows.length == 0) {
        Ext.MessageBox.show({
            title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    var url = "";
    if (_url.indexOf("?") >= 0) {
        url = _url + "&" + id_key + "=" + orows[0].get(id_key);
    } else {
        url = _url + "?" + id_key + "=" + orows[0].get(id_key);
    }
    showHandlerWin(url, title, width, height);
}

function doAdd(url, title, width, height) {
    showHandlerWin(url, title, width, height);
}

/**
*通用表格删除操作
**/
function doDel(grid, url, store, _id) {
    var orows = grid.getSelectionModel().getSelection();
    if (orows.length == 0) {
        Ext.MessageBox.show({
            title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    Ext.MessageBox.confirm('消息提示', '确定删除对象？', function(btn) {
        if (btn == "yes") {
            Ext.Ajax.request({
                url: url,
                params: {
                    _id: orows[0].get(_id)
                },
                method: 'POST',
                success: function(response, options) {
                    Ext.MessageBox.alert('消息提示', '操作成功！');
                    store.loadPage(1);
                },
                failure: function(response, options) {
                    // Ext.MessageBox.alert('消息提示', '操作失败！');
                    Ext.MessageBox.alert('消息提示', Ext.JSON.decode(response.responseText).error);
                }
            });
        }
    })
}

function uploadFile(_form, filename, image_ctl) {
    var url = "/file/upload";
    if (!_form.getForm().findField("attach_file").getValue()) {
        Ext.Msg.alert('消息提示', '请浏览需要上传的文件!');
        return;
    }
    if (filename) {
        url += '?filename=' + filename;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function(form, action) {
            _form.getForm().findField("image_file_id").setValue(action.result.file_id);
            if (image_ctl) {
                if (filename) {
                    image_ctl.setSrc('/_image/' + filename);
                } else {
                    image_ctl.setSrc('/_image/' + action.result.file_id);
                }

            }
            Ext.Msg.alert('消息提示', '上传成功，请按保存继续操作！');
        },
        failure: function(form, action) {
            if (action && action.response && action.response.responseText) {
                Ext.Msg.alert('消息提示', Ext.JSON.decode(action.response.responseText).error);
            } else {
                Ext.Msg.alert('消息提示', '上传失败！');
            }

        }
    });
}

function resizeImage(srcfile, width, filename) {
    Ext.Ajax.request({
        url: '/img_handler/resize',
        params: {
            srcfile: srcfile,
            width: width,
            filename: filename
        },
        method: 'get',
        success: function(response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标成功！');
        },
        failure: function(response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标失败！');
        }
    });
}

function handlerJsonResponse(response, opts){
    var jsonResult = Ext.decode(response.responseText); 
    if(jsonResult.success == false){
        alert(jsonResult.error || jsonResult.msg);
    }else{
        alert("业务处理成功！");
        if(parent && typeof(parent.refresh) === 'function'){
            parent.refresh();
        }
        if(parent && typeof(parent.closeWin) === 'function'){
            parent.closeWin();
        }
    }
}

function handlerJsonResponseAndInitForm(response, opts){
    var jsonResult = Ext.decode(response.responseText); 
    if(jsonResult.success == false){
        return alert(jsonResult.error || jsonResult.msg);
    }
    if(!jsonResult){
        return ;
    }
    for(var i in jsonResult){
        if(dataForm.form.findField(i)){
            dataForm.form.findField(i).setValue(jsonResult[i]);
        }
    }
}